package moreHealthControl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import moreHealthException.IntTextFieldException;
import moreHealthException.StringLengthException;
import moreHealthView.StudentInformationForm;


//Get the Student Information entered such as First Name, LastName, Height, Weight, Age.
public class StudentFormTextBoxListener implements ActionListener
{	
	//CONSTANTS ======================================
	final static int MAX_TEXTFIELD_LENGTH= 11;
	final static int MAX_ATTEMPTS= 4;
	
	
	//ATTRIBUTES ======================================
	
	//The number of attempts Student made entering data in the text field.
	int textFieldAttemptsx_ = 0;
	
	//In case the text field is left blank or string length >10 then use this value. 
	String textFieldDefaultValuex_ = "Default";
	
	//label that associates with the entered text field.
	String textBoxLabelx_="";
	
	//the value entered in the text field.
	String textBoxValuex_ ="";
	
	
	//CONSTRUCTORS ======================================
	//INTENT: the label of the text field in which data was entered.
	//PRE-CONDITION : 
		//1. The label name should be relevant to the textField.
		//2. The label name should not be null
	//POST-CONDITION: 
		//1. Associates this label to the Text Field and its data. 
	public StudentFormTextBoxListener (String texBoxLabel)
	{
		this.textBoxLabelx_ = texBoxLabel;
	}

	
	
	//INTENT: Stores the Student Info entered in a textBox. 
	//PRE-CONDITION : 
		//1. Enter Button should be pressed for this Event to get fired.
		//2. The texField should not be blank or null. 
		//3. The Age, Weight and Height text field needs to be integers.
		//4. Age cannot be more than 99. 
		//5. 20<=Weight<=400
		//6. 1<=Height<=7
		//7. The FirstName and LastName has to be less than 10 characters long.
	//POST-CONDITION: 
		//1. Adds to a Hash-map with a key[ComponentName] and values [Entered data].
		//2. Set the FirstName/LastName to "default" if the Name String length is >=10.
	@Override
	public void actionPerformed(ActionEvent e)
		{
			this.textBoxValuex_ = e.getActionCommand();
			
			if (this.textBoxValuex_ == null)
				{
					//Add default values ===> "Default:FirstName/LastName"
					textBoxValuex_ =textFieldDefaultValuex_ + textBoxLabelx_;
				}
			
			else
				{
					if (this.textBoxLabelx_.equals("firstName") || this.textBoxLabelx_.equals("lastName")){
							
						//Add the text Information if the length is <10 or else take default value. 
						checkStringLength(this.textBoxValuex_);
						}
					
					//TODO: Add filter to check if the data entered are integers only.
					else if (this.textBoxLabelx_.equals("age") || this.textBoxLabelx_.equals("weight") || this.textBoxLabelx_.equals("height") || this.textBoxLabelx_.equals("dobyear"))
						{	
						checkInteger(this.textBoxValuex_);
						
					}
					
				}
		}
	
	
	//INTENT: Adds the Student's first name and last name in the Hash-Map if the string length is < 10 characters  
	//		  or else add a default name. 
	//PRE-CONDITION :
	//POST-CONDITION:
		//1. If the firstname and lastname is >10 then use the default Value. 
		//2. Disable the Ok button if the User Name is > 10 characters. But after the 4th attempt, re-enable the button
		//	 and use the default Name value.
	public void checkStringLength(String testString){
		
		try{
				
		//the name length
		int textlength = testString.length();
		
		
		//Add the Name of the Student which conforms with the length constrain that is <10 characters.
		//Reset the number of attempts for the Student.
		if (textlength<MAX_TEXTFIELD_LENGTH && textFieldAttemptsx_<MAX_ATTEMPTS){
			textFieldAttemptsx_=0;
			StudentInformationForm.jbtOK.setEnabled(true);
			StudentInformationForm.studentInfoFormIp.put(this.textBoxLabelx_, this.textBoxValuex_);
		}
		
		
		//User can enter the Name which less than 10 characters long 4 times. 
		//Ok Button is disabled until user enters a Name < 10 character long.
		else if (textlength >= MAX_TEXTFIELD_LENGTH && textFieldAttemptsx_<=MAX_ATTEMPTS-1) {

			textFieldAttemptsx_++;
			StudentInformationForm.jbtOK.setEnabled(false);
			
			//print the number of attempts. 
			System.out.println("Attempt " + textFieldAttemptsx_);
			}
		
		//If after 5th attempt the Name still does not conform with the string length constraint  
		//then use the Default name value and enable the OK Button for the user to enter other data in the form.
		else if (textFieldAttemptsx_>=MAX_ATTEMPTS) {
			
			System.out.println("You have exceeded your attempt limit. Default Name will be set.");
			StudentInformationForm.jbtOK.setEnabled(true);
			
			//throw exception and assign default value of last and first name. 
			throw new StringLengthException(textFieldDefaultValuex_ + textBoxLabelx_);
			
		}

	  }
		
		catch (StringLengthException stringLengthOverflow ){
			
			//Add the default value to the Hash-Map for Seriousness Factor Calculation.
			StudentInformationForm.studentInfoFormIp.put(this.textBoxLabelx_, stringLengthOverflow.getReturnDefaultValue());
		}
	}
	
	
	//INTENT: Check if the weight, age and height entered by the Student is an Integer. If not set a default value.
	//PRE-CONDITION : 
		//1. The text field should not be blank. 
	//POST-CONDITION:
		//1. If the field does not have integer then set a default values and inform the 
		//   user on the console. 
		//2. Add the Student Data to the Hash Map for Seriousness calculation.
	public void checkInteger (String testString){
		try{
		
		//this will throw a Number format exception. In that case set a default values and inform the user. 
		int convertedString = Integer.parseInt(testString);
		StudentInformationForm.studentInfoFormIp.put(this.textBoxLabelx_, this.textBoxValuex_);
		}
		
		//if not a number then Inform user that default value will be used and to check 
		//the Read me for further instructions. 
		catch (NumberFormatException notInteger){
			try
				{
				
				  throw new IntTextFieldException("0", "Setting default value to 0. Please check the Read me for instruction.  ");
				} catch (IntTextFieldException e)
				{
					System.out.println(e.getReturnErrorMessage());
					StudentInformationForm.studentInfoFormIp.put(this.textBoxLabelx_, e.getReturnDefaultValue());
				}
			
		}
		

	}
}

